// pages/movies/moreMovies/moreMovies.js
import Util from '../../../util/util';

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    key:'',
    movies: {},
    navigateTitle: "",
    requestUrl: "",
    totalCount: 0,
    isEmpty: true,
    lastTime:0
  },
  onRefresh(e){
    if(new Date()-this.data.lastTime<2000){
      return
    }
    this.data.lastTime = new Date();
    let _self =this;
    let nextUrl = this.data.requestUrl +"?start="+this.data.totalCount+"&count=20" ;
    let util = new Util();
    new Promise((resolve,reject)=>{
       wx.showNavigationBarLoading();
       util.getMoviesData(nextUrl,this.data.key,resolve);
        
     })
     .then(res=>{
       _self.processDoubanData(res.data,this.data.key);
       
     })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   // let util = new Util();
   // var refreshUrl = this.data.requestUrl +"?star=0&count=20";
   // this.data.movies = {};
   // this.data.isEmpty = true;
   // this.data.totalCount = 0;
   // //util.http(refreshUrl, this.processDoubanData);
   // new Promise((resolve,reject)=>{
   //   util.getMoviesData(refreshUrl,this.data.key,resolve);
   //    wx.showNavigationBarLoading();
   // })
   // .then(res=>{
   //     let result = util.processDoubanData(res.data,this.data.key);
   //     this.setData({
   //       movies:result.infos
   //    });
   //     this.data.totalCount += 20;
   //     wx.hideNavigationBarLoading();
   //     wx.stopPullDownRefresh()
   // })
   //  console.log(111)
  },
  onGirdTap(e){
    //console.log(e);
    wx.navigateTo({
      url:'../movieDetail/movieDetail?id='+e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const listTitle = {
      inTheaters:'正在热映',
      comingSoon:'即将上映',
      top250:'豆瓣Top250',
    }
    wx.setNavigationBarTitle({
      title: listTitle[options.category]
    })
    const listUrl = {
      inTheaters:'/v2/movie/in_theaters',
      comingSoon:'/v2/movie/coming_soon',
      top250:'/v2/movie/top250',
    }
    let category = options.category;
    let dataUrl = app.globalData.doubanBase+listUrl[category];
    this.data.requestUrl = dataUrl;
    this.data.key = category;
    let url = `${dataUrl}?start=0&count=20`;
    let util = new Util();
    let _self = this;
    new Promise((resolve,reject)=>{
      wx.showNavigationBarLoading();
      util.getMoviesData(url,category,resolve);
    })
    .then(res=>{
       _self.processDoubanData(res.data,category);       
    })
  },

  processDoubanData(...arg){
      let moviesInfo = arg[0].subjects;
      // console.log(arg);
      let infos = [];
      let readyData = {};
    
      moviesInfo.map((val,index,arr)=>{
        let title = val.title.length>6?`${val.title.slice(0, 6)}...`:val.title;
        let imagesUrl = val.images;
        let average = val.rating.average;
        let stars = val.rating.stars;
        let id = val.id;
        stars = new Array(5).fill(false).fill(true,0,stars.slice(0, 1));
        let info = {
          title,
          imagesUrl,
          average,
          stars,
          id
        }
        infos.push(info);
      });

      var totalMovies = {}

      //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
      if (!this.data.isEmpty) {
        totalMovies = this.data.movies.concat(infos);
      }
      else {
        totalMovies = infos;
        this.data.isEmpty = false;
      }
      this.setData({
        movies: totalMovies
      });
      console.log(totalMovies);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.data.totalCount+=20;

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})